package com.buer.storage.controller;

import com.buer.storage.commom.JsonResult;
import com.buer.storage.dto.CorderStorageDto;
import com.buer.storage.dto.DepartmentDto;
import com.buer.storage.dto.DepartmentSelectDto;
import com.buer.storage.dto.WarehouseDto;
import com.buer.storage.entity.Area;
import com.buer.storage.entity.UserEntity;
import com.buer.storage.entity.Warehouse;
import com.buer.storage.service.WarehouseService;
import com.buer.storage.vo.R;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/warehouse")
public class WarehouseController {
    @Resource
    private WarehouseService warehouseService;

    //增加仓库
    @PostMapping("/add.do")
    public R add(@RequestBody WarehouseDto warehouseDto){
        if(warehouseService.insert(warehouseDto)>0){
            return R.setR(1,"增加仓库成功",null);
        }
        return R.setR(0,"添加仓库失败",null);
    }

    //删除仓库
    @GetMapping("/delete.do")
    public R delete(int warehouseId){
        if(warehouseService.delete(warehouseId)>0) {
            return R.setR(1, "删除仓库成功", null);
        }
        return R.setR(0,"删除仓库失败",null);
    }

    //修改仓库
    @PostMapping("/update.do")
    public R update(@RequestBody Warehouse warehouse) {
        if (warehouseService.update(warehouse) > 0) {
            return R.setR(1, "修改仓库成功", null);
        }
        return R.setR(0,"修改仓库失败",null);
    }

    //根据id查询仓库
    @GetMapping("/selectById.do")
    public Warehouse selectById(int warehouseId){
        Warehouse warehouse = warehouseService.selectById(warehouseId);
        return warehouse;
    }

    //查询所有仓库
    @GetMapping("/findAll.do")
    public List<Warehouse> findAll(){
        return warehouseService.findAll();
    }

    //根据仓库id查询库区信息
    @GetMapping("/findAllArea.do")
    public List<Area> findAllArea(int warehouseId){
        List<Area> list = warehouseService.findAllArea(warehouseId);
        return list;
    }

    //根据仓库id查询库位信息
    @GetMapping("/findAllDepartment.do")
    public List<DepartmentDto> findAllDepartment(int warehouseId){
        List<DepartmentDto> department = warehouseService.findAllDepartment(warehouseId);
        return department;
    }

    @GetMapping("/findDepartById.do")
    public List<DepartmentSelectDto> findDepartById(Integer warehouseId){
        List<DepartmentSelectDto> departById = warehouseService.findDepartById(warehouseId);
        return departById;
    }

    @ApiOperation(value = "用户列表")
    @PostMapping("/getUserList")
    public JsonResult getUserList(@RequestBody Map<String,Object> params) {
        PageInfo<Warehouse> entityList = warehouseService.getEntityList(params);
        Map result=new HashMap<>();
        result.put("total",entityList.getTotal());
        result.put("list",entityList.getList());
        return new JsonResult(1,result);
    }
}
